package com.cmfi.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmfi.dao.BOMDao;
import com.cmfi.domain.BOM;
import com.cmfi.service.inter.BOMService;

@Service
public class BOMServiceImpl implements BOMService {
    @Autowired
    private BOMDao bomDao;
    
    Logger log = LoggerFactory.getLogger(getClass());

    public boolean save(BOM bom) {
        return bomDao.save(bom) > 0;
    }
    
    public boolean update(BOM bom, Long id) {
    	bom.setId(id);
        return bomDao.update(bom) > 0;
    }
    
    public boolean delete(Long id) {
        return bomDao.delete(id) > 0;
    }
    
    public BOM getById(Long id) {
        return bomDao.getById(id);
    }
    
    @Override
	public List<BOM> findByPage(Long pageNum, Long pageSize) throws Exception {
		log.info("[BOMServiceImpl][findByPage] pageNum:{},pageSize:{}", pageNum, pageSize);
		Long allNum = bomDao.count();
		long start = (pageNum - 1) * pageSize;
		if (start > allNum) {
			throw new Exception("请求的页已经不存在");
		}
		List<BOM> readMeList = bomDao.findByPage(start, pageSize);
		return readMeList;
	}
    
    @Override
	public List<BOM> findAllBOMs() throws Exception {
		log.info("[BOMServiceImpl][findByPage] getting error.");
		List<BOM> bomList = bomDao.findAllBOMs();
		return bomList;
	}
    
    @Override
   	public long findRoleId(Long id) throws Exception {
   		long roleId = bomDao.findRoleId(id);
   		return roleId;
   	}
    
    @Override
   	public List<BOM> findByUser(Long roleId) throws Exception {
   		List<BOM> limitedBOMList = bomDao.findByUser(roleId);
   		return limitedBOMList;
   	}
}
